package com.attendance.controller;

import com.attendance.common.CommonPage;
import com.attendance.common.CommonResult;
import com.attendance.pojo.Pages;
import com.attendance.service.HomeWorkService;
import com.attendance.vo.ExaminehomeworkVO;
import com.attendance.vo.ExamineworkSituationVO;
import com.attendance.vo.HomeWorkVO;
import com.github.pagehelper.PageHelper;
import lombok.val;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by WYH on 2022/4/22 11:13
 */
@RestController
@RequestMapping("/homework")
public class HomeWorkController {

    @Autowired
    private HomeWorkService homeWorkService;

    @PostMapping("/addhomework")
    @Transactional(rollbackFor = Exception.class)
        public CommonResult add(@RequestBody HomeWorkVO homeWorkVO){
//        1.插入表homework
        try {
            System.out.println(homeWorkVO.getHomId());
            int i = homeWorkService.insertHomework(homeWorkVO);
//        2.获取刚刚插入的homId
            Integer homId = homeWorkVO.getHomId();
            System.out.println(homId);
//        3.根据课程名获取课程名couId
            Integer couId = homeWorkService.selectCourseId(homeWorkVO);
            System.out.println(couId);
            homeWorkVO.setCouId(couId);
//        4.根据小组名获取groId
            Integer groId = homeWorkService.selectProjectGroup(homeWorkVO);
            System.out.println(groId);
            homeWorkVO.setGroId(groId);
//        5.判断作业类型
            if (homeWorkVO.getHomType()=="项目作业"){
//        6.插入homId和groId到project_group_homework
            homeWorkService.insertStudentHomework(homeWorkVO);
//        7.插入groId和couId到project_group_course
            homeWorkService.insertPGC(homeWorkVO);
            } else {
                for(int a = 0;a<homeWorkVO.getList().size();a++){
                    homeWorkVO.setStuName(homeWorkVO.getList().get(a));
//                    8.根据学生姓名查找学生id
                    Integer stuId = homeWorkService.selectStuId(homeWorkVO);
                    homeWorkVO.setStuId(stuId);
                    homeWorkService.insertStuHomework(homeWorkVO);
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
            return CommonResult.failed("新增失败！");
        }
        return CommonResult.success("新增成功！");
    }

    @GetMapping("/selectallwork")
    public CommonResult SelectAllWork(Pages pages){
        List<HomeWorkVO> homeWorkVOS = homeWorkService.SelectAllWork();
        System.out.println(homeWorkVOS);
        if (homeWorkVOS!=null){
            PageHelper.startPage(pages.getPage(),pages.getNum());//分页
           return   CommonResult.success(CommonPage.restPage(homeWorkVOS));
        }else {
            return CommonResult.failed("查询失败");
        }
    }


    @GetMapping("/selectallworkbyhomid")
    @ResponseBody
    public CommonResult SelectAllWorkByHomId(int homId,Pages pages){

        System.out.println("homId"+homId);
        List<HomeWorkVO> homeWorkVOS = homeWorkService.SelectAllWorkByHomId(homId);
        System.out.println("===============");
        System.out.println(homeWorkVOS);
        if (homeWorkVOS!=null){
            PageHelper.startPage(pages.getPage(),pages.getNum());//分页
            return   CommonResult.success(CommonPage.restPage(homeWorkVOS));
        }else {
            return CommonResult.failed("查询失败");
        }
    }


    @PostMapping("/selectstuhomeworkbydy")
    public CommonResult SelectStuHomeworkByDY(@RequestBody HomeWorkVO homeWorkVO,Pages pages){
         List<HomeWorkVO> homeWorkVOS = homeWorkService.SelectStuHomeworkByDY(homeWorkVO);

        System.out.println("===============");

        System.out.println(homeWorkVOS);
        if (homeWorkVOS!=null){
            PageHelper.startPage(pages.getPage(),pages.getNum());//分页
            return   CommonResult.success(CommonPage.restPage(homeWorkVOS));
        }else {
            return CommonResult.failed("查询失败");
        }
    }

    @PostMapping("/selectexaminehomeworkbyconditiondt")
    public CommonResult  SelectExaminehomeworkByConditionDT(ExaminehomeworkVO examinehomeworkVO,Pages pages){
        List<ExaminehomeworkVO> examinehomeworkVOS = homeWorkService.SelectExaminehomeworkByConditionDT(examinehomeworkVO);
        System.out.println("========"+examinehomeworkVOS+"======");
        if (examinehomeworkVOS!=null){
            PageHelper.startPage(pages.getPage(),pages.getNum());//分页
            return   CommonResult.success(CommonPage.restPage(examinehomeworkVOS));
        }else {
            return CommonResult.failed("查询失败");
        }
    }


    @DeleteMapping("/deletehomeworkbyhomid")
    public CommonResult DeleteHomeworkByHomId(int homId){
      int i = homeWorkService.DeleteHomeworkByHomId(homId);
        if (i==0){
            return CommonResult.failed("删除失败");
        }else {
            return CommonResult.success(i,"删除成功");
        }
    }

    @PutMapping("/updateexaminehomeworkbyhomid")
    public CommonResult UpdateExaminehomeworkByhomId(int homId, Float homeScore, String teaComment) {
        final int i = homeWorkService.UpdateExaminehomeworkByhomId(homId, homeScore, teaComment);
        if (i==0){
            return CommonResult.failed("修改失败");
        }else {
            return CommonResult.success(i,"修改成功");
        }

    }
    @GetMapping("/selectworktastbyhomidorteaiddt")
    public  CommonResult SelectworkTastByhomIdOrteaIdDT(int teaId,Pages pages){
       
        List<ExaminehomeworkVO> examinehomeworkVOS = homeWorkService.SelectworkTastByhomIdOrteaIdDT(teaId);
        if (examinehomeworkVOS==null){
            return CommonResult.failed("查询失败");

    }else {
            PageHelper.startPage(pages.getPage(),pages.getNum());//分页
            return   CommonResult.success(CommonPage.restPage(examinehomeworkVOS));
        }
    }
    @GetMapping("/selectworktastbyhomidorteaiddt2")
    public  CommonResult SelectworkTastByhomIdOrteaIdDT2(Pages pages){
         List<ExaminehomeworkVO> examinehomeworkVOS = homeWorkService.SelectworkTastByhomIdOrteaIdDT();
        if (examinehomeworkVOS==null){
            return CommonResult.failed("查询失败");

        }else {
            PageHelper.startPage(pages.getPage(),pages.getNum());//分页
            return   CommonResult.success(CommonPage.restPage(examinehomeworkVOS));
        }
    }

    @GetMapping("/selectshjobcontentbyhomidoorstuid")
    public CommonResult SelectshJobContentByhomIdORstuId(int stuId,Pages pages){
        List<ExaminehomeworkVO> examinehomeworkVOS = homeWorkService.SelectshJobContentByhomIdORstuId(stuId);
        if (examinehomeworkVOS==null){
            return CommonResult.failed("查询失败");

        }else {
            PageHelper.startPage(pages.getPage(),pages.getNum());//分页
            return   CommonResult.success(CommonPage.restPage(examinehomeworkVOS));
        }

    }
    @GetMapping("/selectallexamineworksituation")
    public CommonResult SelectAllExamineworkSituation(Pages pages){
        final List<ExamineworkSituationVO> examineworkSituationVOS = homeWorkService.SelectAllExamineworkSituation();
        if (examineworkSituationVOS==null){
            return CommonResult.failed("查询失败");
        }else {
            PageHelper.startPage(pages.getPage(),pages.getNum());//分页
            return   CommonResult.success(CommonPage.restPage(examineworkSituationVOS));
        }
    }

    @PutMapping("/updateshjobcontentbyhomidorstuid")
    public CommonResult UpdateShJobContentByHomIdOrStuId(String shJobContent,int homId,int stuId){
        final int i = homeWorkService.UpdateShJobContentByHomIdOrStuId(shJobContent, homId, stuId);
        if (i==0){
            return CommonResult.failed("修改失败");
        }else {
            return CommonResult.success(i,"修改成功");
        }
    }

}
